InĀ [1]:
import vectorbt as vbt

vbt.settings['plotting']['use_widgets'] = False
data_exchange = 'binanceusdm'
data_timeframe = '4h'
data_start = '2022-01-01'
data_end = '2022-06-30'
initial_position = 50000
bband_sma = 20
bband_std = 2
bband_bandwidth_thr = 0
InĀ [2]:
btc_price = vbt.CCXTData.download_symbol('BTC/USDT', exchange=data_exchange, timeframe=data_timeframe, start=data_start, end=data_end)
eth_price = vbt.CCXTData.download_symbol('ETH/USDT', exchange=data_exchange, timeframe=data_timeframe, start=data_start, end=data_end)
InĀ [3]:
btc_price['btc_eth'] = btc_price['Close'] / eth_price['Close']
bband = vbt.BBANDS.run(btc_price['btc_eth'], window=bband_sma, alpha=bband_std)
btc_price['bandwidth'] = bband.bandwidth
btc_price['bandwidth'].fillna(0.0, inplace=True)
bband.plot().show()
InĀ [4]:
bband.bandwidth[bband.bandwidth > bband_bandwidth_thr]
bband.bandwidth_stats()
Out[4]:
Start        2021-12-31 16:00:00+00:00
End          2022-06-29 12:00:00+00:00
Period               180 days 00:00:00
Count                             1061
Mean                          0.050249
Std                           0.032681
Min                           0.011449
Median                        0.041099
Max                            0.19165
Min Index    2022-03-14 12:00:00+00:00
Max Index    2022-05-13 04:00:00+00:00
Name: (20, 2, btc_eth), dtype: object
InĀ [5]:
# short BTC long ETH, close at middle
btc_price['upper_crossed_below'] = bband.upper_crossed_below(btc_price['btc_eth']) & (btc_price['bandwidth'] > bband_bandwidth_thr)
btc_price['middle_crossed_below'] = bband.middle_crossed_below(btc_price['btc_eth'])
InĀ [6]:
entries = btc_price['upper_crossed_below']
exits = btc_price['middle_crossed_below']
pf = vbt.Portfolio.from_signals(btc_price['Close'], short_entries=entries, short_exits=exits, max_size=1, init_cash=initial_position)
pf.plot().show()
InĀ [7]:
pf.stats()
Out[7]:
Start                         2021-12-31 16:00:00+00:00
End                           2022-06-29 12:00:00+00:00
Period                                180 days 00:00:00
Start Value                                     50000.0
End Value                                      59037.83
Total Return [%]                               18.07566
Benchmark Return [%]                         -56.076689
Max Gross Exposure [%]                    259074.978204
Total Fees Paid                                     0.0
Max Drawdown [%]                              10.665748
Max Drawdown Duration                  46 days 08:00:00
Total Trades                                         26
Total Closed Trades                                  25
Total Open Trades                                     1
Open Trade PnL                                    192.3
Win Rate [%]                                       44.0
Best Trade [%]                                25.335109
Worst Trade [%]                              -12.456671
Avg Winning Trade [%]                          7.184802
Avg Losing Trade [%]                          -4.611112
Avg Winning Trade Duration    4 days 12:43:38.181818181
Avg Losing Trade Duration     3 days 00:51:25.714285714
Profit Factor                                  1.445079
Expectancy                                     353.8212
Sharpe Ratio                                   1.225649
Calmar Ratio                                   3.756284
Omega Ratio                                    1.107262
Sortino Ratio                                   1.75442
dtype: object
InĀ [8]:
pf = vbt.Portfolio.from_signals(eth_price['Close'], entries=entries, exits=exits, size=btc_price['btc_eth'], init_cash=initial_position)
pf.plot().show()
InĀ [9]:
pf.stats()
Out[9]:
Start                         2021-12-31 16:00:00+00:00
End                           2022-06-29 12:00:00+00:00
Period                                180 days 00:00:00
Start Value                                     50000.0
End Value                                  39425.093046
Total Return [%]                             -21.149814
Benchmark Return [%]                          -69.27921
Max Gross Exposure [%]                            100.0
Total Fees Paid                                     0.0
Max Drawdown [%]                               32.25534
Max Drawdown Duration                 174 days 08:00:00
Total Trades                                         26
Total Closed Trades                                  25
Total Open Trades                                     1
Open Trade PnL                              -527.674877
Win Rate [%]                                       56.0
Best Trade [%]                                19.083301
Worst Trade [%]                               -31.95246
Avg Winning Trade [%]                          6.383879
Avg Losing Trade [%]                          -9.290143
Avg Winning Trade Duration    3 days 00:51:25.714285714
Avg Losing Trade Duration     4 days 12:43:38.181818181
Profit Factor                                  0.725316
Expectancy                                  -401.889283
Sharpe Ratio                                  -0.664883
Calmar Ratio                                  -1.185401
Omega Ratio                                    0.947578
Sortino Ratio                                 -0.933067
dtype: object
InĀ [10]:
# short ETH long BTC, close at middle
btc_price['lower_crossed_above'] = bband.lower_crossed_above(btc_price['btc_eth']) & (btc_price['bandwidth'] > bband_bandwidth_thr)
btc_price['middle_crossed_above'] = bband.middle_crossed_above(btc_price['btc_eth'])
InĀ [11]:
entries = btc_price['lower_crossed_above']
exits = btc_price['middle_crossed_above']
pf = vbt.Portfolio.from_signals(eth_price['Close'], short_entries=entries, short_exits=exits, size=btc_price['btc_eth'], init_cash=initial_position)
pf.plot().show()
InĀ [12]:
pf.stats()
Out[12]:
Start                         2021-12-31 16:00:00+00:00
End                           2022-06-29 12:00:00+00:00
Period                                180 days 00:00:00
Start Value                                     50000.0
End Value                                  84571.357249
Total Return [%]                              69.142714
Benchmark Return [%]                          -69.27921
Max Gross Exposure [%]                     10912.640298
Total Fees Paid                                     0.0
Max Drawdown [%]                              10.795836
Max Drawdown Duration                  74 days 08:00:00
Total Trades                                         19
Total Closed Trades                                  19
Total Open Trades                                     0
Open Trade PnL                                      0.0
Win Rate [%]                                  68.421053
Best Trade [%]                                18.319662
Worst Trade [%]                               -6.876099
Avg Winning Trade [%]                          8.445531
Avg Losing Trade [%]                           -3.90486
Avg Winning Trade Duration    4 days 20:36:55.384615384
Avg Losing Trade Duration               2 days 13:20:00
Profit Factor                                  5.365508
Expectancy                                  1819.545118
Sharpe Ratio                                   3.971468
Calmar Ratio                                  17.627162
Omega Ratio                                    1.436159
Sortino Ratio                                  6.579399
dtype: object
InĀ [13]:
pf = vbt.Portfolio.from_signals(btc_price['Close'], entries=entries, exits=exits, max_size=1, init_cash=initial_position)
pf.plot().show()
InĀ [14]:
pf.stats()
Out[14]:
Start                         2021-12-31 16:00:00+00:00
End                           2022-06-29 12:00:00+00:00
Period                                180 days 00:00:00
Start Value                                     50000.0
End Value                                  28699.116717
Total Return [%]                             -42.601767
Benchmark Return [%]                         -56.076689
Max Gross Exposure [%]                            100.0
Total Fees Paid                                     0.0
Max Drawdown [%]                              45.569827
Max Drawdown Duration                 175 days 00:00:00
Total Trades                                         19
Total Closed Trades                                  19
Total Open Trades                                     0
Open Trade PnL                                      0.0
Win Rate [%]                                  31.578947
Best Trade [%]                                 9.428857
Worst Trade [%]                              -17.162937
Avg Winning Trade [%]                           3.89725
Avg Losing Trade [%]                          -5.795322
Avg Winning Trade Duration              2 days 13:20:00
Avg Losing Trade Duration     4 days 20:36:55.384615384
Profit Factor                                   0.26635
Expectancy                                  -1121.09912
Sharpe Ratio                                  -2.893045
Calmar Ratio                                  -1.482529
Omega Ratio                                      0.7649
Sortino Ratio                                  -3.77497
dtype: object